<?php
header('content-type:application:json;charset=utf8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
error_reporting(E_ERROR);

// 登录微信
function cat_login_oper($_Data, $bot_data) {
	// 微信登录前, 先退出之前相同的微信登录
	if (empty($bot_data['key'])) {
		send_close_pid($bot_data['key']);
	}

	// 产生一个登录的二维码和对应的key值
	$lo_v = Add_Wechat();
	if (empty($lo_v) || empty($lo_v['key'])) {
		return array (
			'state' => -3,
			'msg'	=> '生成二维码失败!',
		);
	}

	// 将key值更新到数据库
	simQuery("UPDATE robot SET wechat_id = '', wechat_uuid_key = '{$lo_v['key']}' WHERE id = {$_Data['id']}");

	// 根据key值获取已经产生的二维码
	$lo_u = Take_QRcode($lo_v['key']);
	if (empty($lo_u) || empty($lo_u['url'])) {
		return array (
			'state' => -4,
			'msg'	=> '获取二维码失败!',
		);
	}
	$imgpath = http_get_file("http://".$bot_data['host'].":1688".$lo_u['url'], BASE_PATH.'img', "jpg");
	$img_base = base64_encode(file_get_contents($imgpath));
	return array (
		'state' 	=> 1,
		'msg'		=> '操作成功',
		'key' 		=> $lo_v['key'],
		'QrBase64'	=> "data:image/jpg;base64,{$img_base}",
	);
}

//	获取微信登录二维码信息
function cat_login_state($_Data, $bot_data) {
	$result = Take_Login($bot_data['wechat_uuid_key']);
	if (is_null($result) || is_null($result['state']) || $result['state'] < -1) {
		return array (
			'state' => -2,
			'msg'	=> "获取二维码状态失败!",
		);
	} else {
		if ($result['state'] == 0) {
			$wx_data = cat_heart_beat($_Data, array('wechat_id' => $result['robot_wxid']));
			simQuery("UPDATE robot SET wechat_nick_name = '{$wx_data['wx_name']}', wechat_id = '{$result['robot_wxid']}', wechat_head_url = '{$wx_data['wx_head_url']}' WHERE id = {$_Data['id']}");
			return array (
				'state'		=> 1,
				'msg'		=> "操作成功!",
				'WxId'		=> $result['robot_wxid'],
				'NickName'	=> $wx_data['wx_name'],
				'HeadUrl'	=> $wx_data['wx_head_url'],
			);
		} else {
			return array (
				'state' => 0,
				'msg'	=> "正在登录!",
				'key'	=>  $result['key'],
			);
		}
	}
}

// 退出微信
function cat_exit_oper($_Data, $bot_data) {
	$result = send_close_pid($bot_data['wechat_uuid_key']);
	if (empty($result) || $result['state'] == -1) {
		simQuery("UPDATE robot SET wechat_uuid_key = '', wechat_nick_name = '', wechat_id = '', wechat_head_url = '' WHERE id = {$_Data['id']}");
		return array (
			'state' => 1,
			'msg'	=> '微信登录信息已清空,请登录服务器进行退出操作,确定已经退出可不进行任何操作!',
		);
	} else {
		simQuery("UPDATE robot SET wechat_uuid_key = '', wechat_nick_name = '', wechat_id = '', wechat_head_url = '' WHERE id = {$_Data['id']}");
		return array (
			'state' => 1,
			'msg'	=> '退出成功!',
		);
	}
}

// 获取微信登录信息
function cat_heart_beat($_Data, $bot_data) {
	$result = Take_wechat();
	$wx_data;
	foreach ($result as $k => $v) {
		if ($v['wxid'] == $bot_data['wechat_id']) {
			$wx_data = $result[$k];
		}
	}

	if (empty($wx_data)) {
		simQuery("UPDATE robot SET wechat_uuid_key = '', wechat_nick_name = '', wechat_id = '', wechat_head_url = '' WHERE id = {$_Data['id']}");
		return array (
			'state' => -3,
			'msg'	=> '没有该登录信息!',
		);
	}

	return array (
		'state'			=> 1,
		'msg'			=> '获取消息成功!',
		'wx_id' 		=> $wx_data['wxid'],
		'wx_name'		=> $wx_data['nickname'],
		'wx_head_url' 	=> $wx_data['headimgurl'],
		'wx_ground_url'	=> $wx_data['backgroundimgurl'],
		'login_time'	=> $wx_data['login_time'],
	);
}

?>